﻿/* Write a program that reads an integer number and calculates and prints its square root. If the number is invalid or negative, print "Invalid number". In all cases finally print "Good bye". Use try-catch-finally.
*/

using System;
using System.Globalization;
using System.Threading;

namespace _1.CalcSQRoot
{
    class CalcSQRoot
    {
        static void Main()
        {
            //globalization settings
            Thread.CurrentThread.CurrentCulture = CultureInfo.InvariantCulture;
            int N;
            Console.Write("Enter your input: ");
            try
            {
                N = Int32.Parse(Console.ReadLine());
                double sq = Math.Sqrt(N);
                Console.WriteLine("The square root of {0} is {1:0.000}", N, sq);
            }
            catch (ArgumentNullException ne)
            {
                throw new FormatException(string.Format("Invalid number detected. Please check the user input for proper values\r\n{0}\r\nValid range is from {1} to {2}", ne.Message, int.MinValue, int.MaxValue));
            }
            catch (FormatException fe)
            {
                throw new FormatException(string.Format("Invalid number detected. Please check the user input for proper values\r\n{0}\r\nValid range is from {1} to {2}", fe.Message, int.MinValue, int.MaxValue));
            }
            catch (OverflowException oe)
            {
                throw new OverflowException(string.Format("Invalid number detected. Please check the user input for proper values\r\n{0}\r\n\tValid range is from {1} to {2}", oe.Message, int.MinValue, int.MaxValue));
            }
            finally
            {
                Console.WriteLine("Good bye");
            }
        }
    }
}
